//
//  RecommendedController.m
//  EasyToTesco2.0
//
//  Created by 杨森 on 16/6/23.
//  Copyright © 2016年 EasyToTesco. All rights reserved.
//

#import "RecommendedController.h"
#import "homeCollectionViewCell.h"
#import "UINavigationBar+Awesome.h"

#import "GoodListsModel.h"
#import "ShopDetailsController.h"

//间距
#define MAR_WIDTH 5

#define NAVBAR_CHANGE_POINT 50

@interface RecommendedController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource>
{
    TopPullRefreshAnaimation *gifHeader;
    MBProgressHUD *progressHUD;
    NSUInteger refreshIndex;
    NSDictionary *_urlDict;
}

@property(nonatomic,strong)UICollectionView *recommendView;
@property(nonatomic,strong)NSMutableArray *goodsListArray;

@end

@implementation RecommendedController

static NSString * const reuseIdentifier = @"secondReuseIdentifier";


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addCollectionView];
    
    refreshIndex = 1;
    //上啦刷新
    [self addPullUpdateData];
    
    [self getGoodsListsRequestData];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
}

- (void)addCollectionView
{
    UICollectionViewFlowLayout *flowlayout = [[UICollectionViewFlowLayout alloc] init];
    flowlayout.minimumLineSpacing = 2;
    flowlayout.minimumInteritemSpacing = 0;
    flowlayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    UICollectionView *recommendView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,WIDTH, HEIGHT-64) collectionViewLayout:flowlayout];
    self.recommendView = recommendView;
    self.recommendView.delegate = self;
    self.recommendView.dataSource = self;
    recommendView.backgroundColor = BACKGROUND_COLOR;
    [self.view addSubview:recommendView];
    
    [self.recommendView registerClass:[homeCollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
}

- (void)addPullUpdateData
{
    self.recommendView.mj_footer = [TopPullRefreshAnaimation footerWithRefreshingTarget:self refreshingAction:@selector(getGoodsListsLoadMoreData)];
}

- (void)getGoodsListsLoadMoreData
{
    refreshIndex ++;
    [self getGoodsListsRequestData];
    [self.recommendView.mj_footer endRefreshing];
}


#pragma mark >>>>>>>>>获取商品列表
- (void)getGoodsListsRequestData
{
    //菊花加载
    progressHUD = [MBProgressHUD loadingInformationToView];
    [self.view addSubview:progressHUD];
    
    if (refreshIndex == 1) {
        [self.goodsListArray removeAllObjects];
    }
    NSString *P = [NSString stringWithFormat:@"%ld",refreshIndex];
    
    //精品推荐
    NSString *url = @"/api/goods/appGoodLists";
    NSDictionary *params = @{@"p":[NSString stringWithFormat:@"%@",P],
                             @"recommend":@"3"
                             };
    
    //活动列表
    if (_is_actPush) {
        params = @{@"p":[NSString stringWithFormat:@"%@",P],@"ids":_product_id};
        url = @"/api/goods/lists";
        
    }
    //农特产品
    if (_is_Agricultural) {
        url = @"/api/goods/lists/fffid/1006";
        params = @{@"p":[NSString stringWithFormat:@"%@",P]};
    }
    //积分特惠
    if (_is_teHui) {
        url = @"/api/goods/lists/ffid/1004";
        params = @{@"p":[NSString stringWithFormat:@"%@",P]};
    }
    
    if (_is_tour) {
        url = @"/api/goods/lists/ffid/1018";
        params = @{@"p":[NSString stringWithFormat:@"%@",P]};
    }
    
    [[NetWorkSharedTools sharedNetWorkTools] objectWithURLDict:params AndMenthodName:url completeBlock:^(id object) {
        if (JudgeBackDataCode) {
            
            NSLog(@"111111%@",object);
            NSDictionary *tempDict = object[@"data"];
            NSArray *arr = [tempDict objectForKey:@"list"];
            
            NSLog(@"%@",_urlDict);
            for (NSDictionary *dict in arr) {
                GoodListsModel *goodListsModel = [[GoodListsModel alloc] initWithDictionary:dict error:nil];
                if (goodListsModel) {
                    [self.goodsListArray addObject:goodListsModel];
                    
                }
            }
            
        }else{
            //没有更多数据
            [_recommendView.mj_footer endRefreshingWithNoMoreData];
        }
        //菊花隐藏
        [progressHUD hide:YES];
        [progressHUD removeFromSuperViewOnHide];
        [self.recommendView reloadData];
        
    }];
    
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.goodsListArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    homeCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    GoodListsModel *goodListsModel = self.goodsListArray[indexPath.item];
    
    NSString *string = [NSString stringWithFormat:@"%@",goodListsModel.picurl];
    [cell.productImage sd_setImageWithURL:[NSURL URLWithString:string] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    cell.productImage.contentMode = UIViewContentModeScaleToFill;
    
    cell.productName.text = goodListsModel.title;
    cell.price.text = [NSString stringWithFormat:@"专享价: %@",goodListsModel.huiyuanjia];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
    
}

#pragma mark >>>>>>>>collectionView 代理
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GoodListsModel *goodListsModel = self.goodsListArray[indexPath.item];
    ShopDetailsController *detailsVC = [[ShopDetailsController alloc] init];
    detailsVC.goodsId = goodListsModel.replaceID;
    [self.navigationController pushForm:self.navigationController pushToAndHidden:detailsVC];
}

//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake(WIDTH/2-2, HEIGHT/2.8);
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    
    return UIEdgeInsetsMake(1,1,1,1);
}


- (NSMutableArray *)goodsListArray {
    if (!_goodsListArray) {
        _goodsListArray = [NSMutableArray array];
    }
    return _goodsListArray;
}

@end
